package com.pinda.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pinda.base.entity.PdCourierScop;
import com.pinda.commonutils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import com.alibaba.fastjson.JSON;
import com.pinda.base.user.CourierScopeDto;
import com.pinda.commonutils.utils.Result;
import com.pinda.service.IPdCourierScopeService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 快递员业务范围表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-courier-scop")
public class PdCourierScopController {
    @Autowired
    private IPdCourierScopeService courierScopeService;



    /**
     * 批量保存快递员业务范围
     *
     * @param dtoList 快递员业务范围信息
     * @return 返回信息
     */
    @PostMapping("/courier/batch")
    public Result batchSaveCourierScope(@RequestBody List<CourierScopeDto> dtoList) {
        courierScopeService.batchSave(dtoList.stream().map(dto -> {
            PdCourierScop scope = new PdCourierScop();
            BeanUtils.copyProperties(dto, scope);
            if (!ObjectUtils.isEmpty(dto.getMutiPoints())) {
                scope.setMutiPoints(JSON.toJSONString(dto.getMutiPoints()));
            }
            return scope;
        }).collect(Collectors.toList()));
        return Result.ok();
    }

    /**
     * 根据用户id查询
     * @param userId
     * @return
     */
    @GetMapping("findPdCourierScopByUserId")
    public com.pinda.commonutils.Result findPdCourierScopByUserId(String userId){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        PdCourierScop one = courierScopeService.getOne(queryWrapper);
        return com.pinda.commonutils.Result.builder().data(one).code(StatusCode.OK).message("查询成功").build();
    }
    @PostMapping("fanweisave")
    public void fanweisave(@RequestBody  PdCourierScop pdCourierScop){
        courierScopeService.fanweisave(pdCourierScop);
    }
    @PostMapping("fanweibanduan")
    public PdCourierScop fanweibanduan(@RequestBody  PdCourierScop pdCourierScop){
       return courierScopeService.fanweibanduan(pdCourierScop);
    }
    @PostMapping("fanweiupdate")
    public void fanweiupdate(@RequestBody  PdCourierScop pdCourierScop){
        courierScopeService.fanweiupdate(pdCourierScop);
    }


}
